/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "PhysicsObject.h"

PhysicsObject::PhysicsObject(void)
{
    m_fMass = 0.0f;
    m_fFriction = 0.0f;
    m_vVelocity.ZeroOut();
    m_vPosition.ZeroOut();
    m_vAcceleration.ZeroOut();
    m_vDestination.ZeroOut();
}

PhysicsObject::~PhysicsObject(void)
{
}

/** Update this object */
void PhysicsObject::Update( float deltaTime )
{
    if ( m_fMass == 0.0f )
        return;

    //if ( m_vDestination.Length() )
    //{
    //    Vector2f destination =  m_vDestination - m_vPosition;
    //    Vector2f velocity = destination;
    //    velocity.Normalize();

    //    Vector2f moveTo = velocity * 180.0f * deltaTime;
    //    if ( destination.Length() <= 2 )
    //    {
    //        m_vDestination.ZeroOut();
    //    } else m_vPosition = m_vPosition + moveTo;
    //    return;
    //}

    Vector2f force(0, 0);
    type_VectorList::iterator it = m_forces.begin();
    while( it != m_forces.end() )
    {
        Vector2f &f = (*it);
        f = f * m_fFriction;
        if ( f.Length() < 1 )
        {
            it = m_forces.erase( it );
        }
        else
        {
            force = force + f;
            ++it;
        }
    }

    // Now add impulses
    while( !m_impulses.empty() )
    {
        force = force + m_impulses.back();
        m_impulses.pop_back();
    }

    // now calculate our new pos
    // a = F/m
    m_vAcceleration = force / m_fMass;
    // v = v0 + a*t
    m_vVelocity = /*m_vVelocity +*/ (m_vAcceleration * deltaTime);
    m_vPosition = m_vPosition + (m_vVelocity /** deltaTime*/);

}